package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.SalesmanApply;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.SalesmanApplyMapper;
import com.rm.zeyou.mapper.SalesmanMapper;
import com.rm.zeyou.mapper.UserMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.SalesmanApplyService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.ApplySalesmanVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 业务员申请表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-22 14:47:24
 */
@Service
public class SalesmanApplyServiceImpl extends ServiceImpl<SalesmanApplyMapper, SalesmanApply> implements SalesmanApplyService {
    @Resource
    private SalesmanApplyMapper salesmanApplyMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SalesmanMapper salesmanMapper;


    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> applySalesman(User user , ApplySalesmanVo applyVo) {
        //判断是否有正在审核或审核成功的数据
        int successCount = salesmanApplyMapper.selectCount(new LambdaQueryWrapper<SalesmanApply>()
                .eq(SalesmanApply::getUserId, user.getId())
                .ne(SalesmanApply::getAuditStatus, NumberConstants.TWO_CONSTANTS)
        ).intValue();
        if (successCount > 0) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.AUDITING);
        }
        if (user.getUserIdentity().equals(NumberConstants.TWO_CONSTANTS)){
            throw new ErrorCodeException(ErrorCodeExceptionEnum.IS_CHANNEL);
        }
        //如果填写上级手机号
        //已有上级  报错
        //没有上级  查询填的上级手机号是否正确  //正确 存表  错误报错
        if (!CheckEmptyUtil.isEmpty(applyVo.getPartnerTel())) {

            if (!CheckEmptyUtil.isEmpty(user.getUserInvitedCode())) {
                throw new ErrorCodeException(ErrorCodeExceptionEnum.IS_BIND_PARTNER);
            }
            User partnerUser = userMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getUserTel, applyVo.getPartnerTel()));
            if (CheckEmptyUtil.isEmpty(partnerUser)) {
                throw new ErrorCodeException(ErrorCodeExceptionEnum.PARTNER_EMPTY);
            }

        }
        SalesmanApply salesmanApply = new SalesmanApply();
        salesmanApply.setUserId(user.getId());
        salesmanApply.setName(applyVo.getName());
        salesmanApply.setTel(applyVo.getTel());
        salesmanApply.setPartnerTel(applyVo.getPartnerTel());
        int insert = salesmanApplyMapper.insert(salesmanApply);
        return ApiRestResponse.success(insert);
    }
}
